import re
list_key_pattern=re.compile(r'哪些|哪几个(和|\、|,)?')
count_key_pattern=re.compile(r'(计数|数量|个数|数目|有几家|多少家|有几部|多少个|有多少|有几个)(和|\、|,)?')
sum_key_pattern=re.compile(r'(总和|之和|总量|总额|总的|全体|全部|合计)(和|\、|,)?')
avg_key_pattern=re.compile(r'(平均值|均值|平均)(和|\、|,)?')
max_key_pattern=re.compile(r'(最大值|极大值|极大)(和|\、|,)?')
min_key_pattern=re.compile(r'(最小值|极小值|极小)(和|\、|,)?')
pattern_list=[list_key_pattern,count_key_pattern,sum_key_pattern,avg_key_pattern,max_key_pattern,min_key_pattern]
def break_ques(dimension_dict):
    ques=dimension_dict['question']
    sel_name_list=[sel['name'] for sel in dimension_dict['sel']]
    list_flag=False
    # count_flag=False
    contradiction_word_list=[]
    # for i in range(len(sel_name_list)):
    #     if '名称' in sel_name_list[i]:
    #         list_word=sel_name_list[i]
    #         contradiction_word_list.append(list_word)
    # if not list_flag:
    #     list_word=list_key_pattern.search(ques)
    #     if list_word:
    #         list_word=list_word[0]
    #         contradiction_word_list.append(list_word)
    for pattern in pattern_list:
        pattern_word=pattern.search(ques)
        if pattern_word:
            contradiction_word_list.append(pattern_word[0])
        # count_flag=True
    # print(contradiction_word_list)
    if len(contradiction_word_list)>1:
        return delete_others(dimension_dict,contradiction_word_list)
    else:
        return [dimension_dict]
def delete_others(dimension_dict,delete_word_list):
    ques=dimension_dict['question']
    dict_list=[]
    for i in range(len(delete_word_list)):
        ques_tmp=ques[:]
        new_dict=dimension_dict.copy()
        for j in range(len(delete_word_list)):
            if j!=i:
                ques_tmp=re.sub(delete_word_list[j],'',ques_tmp)
                # if any(key_word in delete_word_list[j] for key_word in['哪些','哪几个','名称']):
                #     # print("检测到名称")
                    
                #     new_dict['sel']=dimension_dict['sel'][:]
                #     for k in range(len(dimension_dict['sel'])):
                #         if "名称" in dimension_dict['sel'][k]['name']:
                #             # if new_dict['sel'][k]['name'] in ques_tmp:
                #             #     ques_tmp=re.sub(new_dict['sel'][k]['name'],'',ques_tmp)
                #             # else:
                #             #     ques_tmp=re.sub(delete_word_list[j],'',ques_tmp)
                #             new_dict['sel'].pop(k)
                # else:
                #     ques_tmp=re.sub(delete_word_list[j],'',ques_tmp)
        if ques_tmp[-1] in [',','和','、'] and not ques_tmp[-2] in ['总','之']:
            ques_tmp=ques_tmp[:-1]
        new_dict['question']=ques_tmp
        # print(new_dict)
        dict_list.append(new_dict)
    return dict_list
if __name__ == '__main__':
    # test_ques='深圳分部超过十万的合同名称和数量'
    # test_ques='深圳分部合同金额最小值'
    # test_name_list=['销售分部','合同金额']
    # test_id_list=[]
    test_dict={
    "question": "今年9月合同金额大于十万的合同名称和数量",
    "sel": [
        {
            "member_amount": 1,
            "name": "月",
            "timeLevel": "month",
            "id": "AUGMENTED_DATASET_LEVEL.I8a8ae5ca0178549554951b9501785cefe3f00058.MT_DIMENSION_FIELD.02de0a83c07e6c06b5f6dc8d09f9fce7-C_DATE_Month-LEVEL-1648634671682",
            "type": "STRING"
        },
        {
            "member_amount": 0,
            "name": "合同金额",
            "timeLevel": "",
            "id": "AUGMENTED_DATASET_MEASURE.I8a8ae5ca0178549554951b9501785cefe3f00058.MT_METRICS.I8a8ae5ca0178549554951b9501785d983aaa005e",
            "type": "DOUBLE"
        },
        {
            "member_amount": 0,
            "name": "合同名称",
            "timeLevel": "",
            "id": "AUGMENTED_DATASET_MEASURE.I8a8ae5ca0178549554951b9501785cefe3f00058.MT_METRICS.I8a8ae5ca0178549554951b9501785d983aaa005e",
            "type": "DOUBLE"
        },
    ],
    }
    break_ques(test_dict)